package cn.loioi.config;

import org.activiti.engine.HistoryService;
import org.activiti.engine.ManagementService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.activiti.spring.ProcessEngineFactoryBean;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import com.alibaba.druid.pool.DruidDataSource;

import cn.loioi.activiti.ActivitiService;

/**
 * <br>
 * (c) Copyright 2016 by <br>
 * @description :工作流配置 <br>
 * @file_name :ActivitiConfig.java <br>
 * @system_name :multi_project_activiti <br>
 * @author :yyzh <br>
 * @创建时间 :2016年6月1日 上午10:43:29
 */
@Order(5)
@Configuration
public class ActivitiConfig {
	@Autowired
	@Qualifier("masterDataSource")
	private DruidDataSource druidDataSource;

	/**
	 * 设置工作流数据库
	 * @return
	 */
	@Bean
	public StandaloneProcessEngineConfiguration processEngineConfiguration() {
		StandaloneProcessEngineConfiguration configuration = new StandaloneProcessEngineConfiguration();
		configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
		configuration.setDataSource(druidDataSource);
		configuration.setTransactionFactory(new JdbcTransactionFactory());
		configuration.setJobExecutorActivate(true);
		return configuration;
	}

	@Bean
	public ProcessEngine processEngine() throws Exception {
		ProcessEngineFactoryBean factoryBean = new ProcessEngineFactoryBean();
		factoryBean.setProcessEngineConfiguration(processEngineConfiguration());
		return factoryBean.getObject();
	}

	@Bean
	public RepositoryService repositoryService() throws Exception {
		return processEngine().getRepositoryService();
	}

	@Bean
	public RuntimeService runtimeService() throws Exception {
		return processEngine().getRuntimeService();
	}

	@Bean
	public TaskService taskService() throws Exception {
		return processEngine().getTaskService();
	}

	@Bean
	public HistoryService historyService() throws Exception {
		return processEngine().getHistoryService();
	}

	@Bean
	public ManagementService managementService() throws Exception {
		return processEngine().getManagementService();
	}

	@Bean
	public org.activiti.engine.IdentityService identityService() throws Exception {
		return processEngine().getIdentityService();
	}

	@Bean
	public ActivitiService activitiService() throws Exception {
		ActivitiService activitiService = new ActivitiService();
		activitiService.setProcessEngine(processEngine());
		activitiService.setRuntimeService(runtimeService());
		activitiService.setRepositoryService(repositoryService());
		activitiService.setHistoryService(historyService());
		activitiService.setIdentityService(identityService());
		activitiService.setTaskService(taskService());
		return activitiService;
	}
}